#!/usr/bin/perl -w # for syntax highlighting
# CSS-V1

# Take all the args and update portal ... and then
# redirect to page in the LP parameter.
use gri_secmgr qw(get_cookie_value redirect_header);
use gri_user_alert_config;
use gri_database;

sub mm_form_docrepos_add_group_process {
	my $str="";

	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $DB=new gri_database("$db_dir/gri.db");
	
	my $group_name=$::PARAM{name};
	my $group_description=$::PARAM{description};
	$group_name="" if !defined($group_name);
	$group_description="" if !defined($group_description);

	# Get details of all users...

			
	#################################################################
	# Check to ensure we do not already have a section of this	#
	# name and raise an error if we do.				#
	#################################################################
	{
		my ($x,$y)=$DB->get_group(group=>$group_name);
		if($x==1) {
			my $serror="Error Adding New Group";
			my $error="Specified Document Repository Group already exists.";
			my $link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
			if(!exists($::PARAM{lp})) {
				# We need to encode the current data for repopulation into the form!
				my $s="";
				$s.="group_name=".string_to_hex($group_name);
				$s.="::group_description=".string_to_hex($group_description);
				$link.=",previouspage=form_docrepos_admin_add_group::$s";
			}
			redirect_header(path => $link);
			exit;
		}
	}

	#################################################################
	# Everything should be in place, so now add the group ... 	#
	# setting the groups if appropriate.				#
	#################################################################

WRITERECORD: {
		$serror=$error=undef;
		my ($x,$y)=$DB->add_group(
			group=>$group_name,
			description=>$group_description);
		if($x!=1) {
			$serror="Error Adding New Group";
			$error="Unable to write to database - error RC was $x.";
			last WRITERECORD;
		}
		# Ok - if the basic record has been written we should be able to 
		# set the users in the group as necessary.

		for my $cparam (keys %::PARAM) {
			if($cparam =~ /^user_(.*)/) {
				my $user=$1;
				($x,$y)=$DB->add_user_to_group(group=>$group_name,user=>$user);
				if($x!=1) {
					$serror="Error Adding user to Group";
					$error="An error occurred whilst attempting to add user '$user' to the group '$group_name' reader list - RC=$x";
					last WRITERECORD;
				}
			}
		}
	}

	if(defined($serror)) {
		my $link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		if(!exists($::PARAM{lp})) {
			# We need to encode the current data for repopulation into the form!
			my $s="";
			$s.="group_name=".string_to_hex($group_name);
			$s.="::sec_description=".string_to_hex($sec_description);
			$link.=",previouspage=form_docrepos_admin_add_group::$s";
		}
		redirect_header(path => $link);
		exit;
	}

	my $link=link_to_str("","","/admin_page","");
	redirect_header(path => $link);
	exit;
}

